package com.wn55.hrms.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Employee;
import com.wn55.hrms.mapper.DeptMapper;
import com.wn55.hrms.mapper.EmployeeMapper;
import com.wn55.hrms.service.vo.AgeVo;
import com.wn55.hrms.service.vo.DeptNumVo;
import com.wn55.hrms.mapper.RoleMapper;
import com.wn55.hrms.service.vo.EmployeeVo;
import com.wn55.hrms.service.vo.JobStatusVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class EmployeeService {
    private EmployeeMapper employeeMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }


    public PageInfo<Employee> listByCondition(Integer currentPage, Integer pageSize, String name) {
        PageHelper.startPage(currentPage, pageSize);
        List<Employee> list = employeeMapper.listByCondition(name);
        return new PageInfo<>(list);
    }

    public Employee login(String name) {
        return employeeMapper.selectManagerByName(name);
    }

    public void updateEmployee(Employee employee) {
        employeeMapper.updateByPrimaryKey(employee);
    }

    public PageInfo<EmployeeVo> listManagerByCondition(Integer currentPage, Integer pageSize, String name,Integer empType,Integer pId) {
        PageHelper.startPage(currentPage, pageSize);
        List<EmployeeVo> list = employeeMapper.listManagerByCondition(name,empType,pId);
        return new PageInfo<>(list);
    }

    @Transactional
    public void modifyManager(EmployeeVo employeeVo) {
        employeeMapper.updateManager(employeeVo);
        employeeMapper.deleteRoleByEmpId(employeeVo.getId());
        List<Integer> list =new ArrayList<>();
        for (String s : employeeVo.getRoleIds().split(":")) {
            list.add(Integer.parseInt(s));
        }
        employeeMapper.insertRoleById(list,employeeVo.getId());
    }

    public void lockedManager(Integer eId, Integer locked) {
        employeeMapper.lockedManager(eId,locked);
    }

    public void addEmployee(Employee employee) {
       int eid= employeeMapper.insert(employee);
       //根据插入的id来给该员工赋予角色

        //根据员工职位id查出职位所拥有的角色，即员工职位名

        //给员工赋予该就角色
    }

    public void deleteEmployee(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    public Employee getEmpById(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    public PageInfo<EmployeeVo> listInternByCondition(Integer currentPage, Integer pageSize, String name,Integer deptId, Integer pId) {
        PageHelper.startPage(currentPage, pageSize);
        List<EmployeeVo> list = employeeMapper.listInternByCondition(name,deptId,pId);
        return new PageInfo<>(list);
    }

    public List<EmployeeVo> listEmpByCondition(Integer deptId, Integer positionId) {
        return employeeMapper.listEmpByCondition(deptId,positionId);
    }

    public AgeVo initAgeData() {
        return employeeMapper.initAgeData();
    }

    public JobStatusVo initJobStatusData() {
        return employeeMapper.initJobStatusData();
    }

    public DeptNumVo initDeptData() {
        return employeeMapper.initDeptData();
    }

    public List<EmployeeVo> initImgData() {
        return employeeMapper.initImgData() ;
    }
}
